/* Author      : Mark Kahn (cwolves@gmail.com)
 * Description : "Fixes" code spacing by aligning = signs, :'s, etc. Makes code much easier to read.
 * 
 * Notes:
 * * You must download the javascript file from the google repository to your local machine
 * 
 * Command : cscript //NoLogo "c:\path to file\file.js"
 * Check   : "Run as text filter"
 * 
 * Example:
 * 
 * Input:
 * ------
 * .text_blue_small {
 * 	font-size:11px;
 * 	color:#4C82D8;
 * 	text-decoration:none;
 * 	padding-left:10px;
 * }
 * #wrapper {
 * 	background:white;
 * 	width:800px;
 * 	margin:0 auto;
 * 	border:1px solid #999999;
 * 	padding:0px;
 * }
 * ------
 * 
 * Output:
 * ------
 * .text_blue_small {
 * 	font-size       : 11px;
 * 	color           : #4C82D8;
 * 	text-decoration : none;
 * 	padding-left    : 10px;
 * }
 * #wrapper {
 * 	background      : white;
 * 	width           : 800px;
 * 	margin          : 0 auto;
 * 	border          : 1px solid #999999;
 * 	padding         : 0px;
 * }
 * ------
 * 
 * 
 * 
 * Input:
 * ------
 * $someArray = array(
 * 	'field'=>'data',
 * 	'another field'=>'some more data',
 * 	'etc'=>'etc'
 * );
 * ------
 * 
 * Output:
 * ------
 * $someArray = array(
 * 	'field'         => 'data',
 * 	'another field' => 'some more data',
 * 	'etc'           => 'etc'
 * );
 * ------
 * 
 * 
 * 
 * Input:
 * ------
 * a=5;
 * b=20;
 * dog=30;
 * some_really_really_long_variable_name=40;
 * ------
 * 
 * Output:
 * ------
 * a                                     = 5;
 * b                                     = 20;
 * dog                                   = 30;
 * some_really_really_long_variable_name = 40;
 * ------
 */


// settings

var ignore      = ['<%', '%>', '<?', '?>'];
var splitVals   = ['===', '!==', '==', '=>', '!=', '+=', '-=', '*=', '/=', '->', '=', ':', '?'];
var noPreSpace  = ['?'];
var noPostSpace = ['?'];
var quoteChars  = ['"', "'"];
var escapeChar  = '\\';

// do not edit below this line

var stdin       = WScript.StdIn;
var stdout      = WScript.StdOut;
var input       = stdin.ReadAll();

var maxSplit    = 0;
var splits      = [];
var lines       = input.split('\n');
var output      = '';
var currentLine, currentSplit, inQuote, spaces, j, currentLineString, i, isSplit;

String.prototype.trim  = function() {return this.replace(/^ +/,'').replace(/ +$/,'');}
Array.prototype.exists = function(e){return this.find(e)>=0;}
Array.prototype.find   = function(e){for(var i=0;i<this.length;i++)if(this[i]==e)return i;return -1;}

for(var i=0; i<splitVals.length; i++) if(splitVals[i].length>maxSplit) maxSplit = splitVals[i].length;

function doLoop(mid, end){
	for(i=0; i<lines.length; i++){
		currentLine  = lines[i];
		currentSplit = splits;
		inQuote      = '';
		curStr       = '';
		spaces       = 0;

		loop2:
		for(j=0; j<currentLine.length; j++){
			currentLineString = currentLine.substr(j, maxSplit);

			for(var k=0; k<ignore.length; k++){
				var splitValue = ignore[k];
				if(currentLineString.indexOf(splitValue)==0){
					j      += splitValue.length-1;
					curStr += splitValue;
					continue loop2;
				}
			}

			if(currentLineString.substr(0, 1) == escapeChar){
				j++;
				curStr += currentLineString.substr(0, 2);
				continue;
			}
			if((inQuote=='') && (quoteChars.exists(currentLineString.substr(0, 1)))){
				inQuote = currentLineString.substr(0, 1);
				curStr += currentLineString.substr(0, 1);
				continue;
			}
			if(currentLineString.substr(0, 1)==inQuote){
				inQuote = '';
				curStr += currentLineString.substr(0, 1);
				continue;
			}
			if(inQuote!=''){
				curStr += currentLineString.substr(0, 1);
				continue;
			}

			isSplit = false;

			if(mid) mid();

			if(!isSplit) curStr += currentLineString.substr(0, 1);
		}

		if(end) end();
	}
}

function x(){
	for(var k=0; k<splitVals.length; k++){
		var splitValue = splitVals[k];
		if(currentLineString.indexOf(splitValue)==0){
			j += splitValue.length-1;
			if(!currentSplit[splitValue]){
				currentSplit[splitValue] = {max:curStr.trim().length, ary:[]};
			} else {
				if(curStr.trim().length>currentSplit[splitValue].max) currentSplit[splitValue].max = curStr.trim().length;
			}
			curStr       = '';
			currentSplit = currentSplit[splitValue].ary;
			isSplit      = true;
			break;
		}
	}
}

function y(){
	for(var k=0; k<splitVals.length; k++){
		var splitValue = splitVals[k];
		if(currentLineString.indexOf(splitValue)==0){
			var preSpace  = ' ';
			var postSpace = ' ';
			for(var l=0; l<noPreSpace.length;  l++) if(noPreSpace[l] ==splitValue) preSpace  = '';
			for(var l=0; l<noPostSpace.length; l++) if(noPostSpace[l]==splitValue) postSpace = '';

			j      += splitValue.length-1;
			var aLen = currentSplit[splitValue].max - curStr.trim().length + 1;
			if(aLen<0) alert(currentLine.substr(j));
			output += curStr.trim() + new Array(aLen).join(' ') + preSpace + splitValue + postSpace
			if(!currentSplit[splitValue]){
				currentSplit[splitValue] = {max:curStr.trim().length, ary:[]};
			} else {
				if(curStr.trim().length>currentSplit[splitValue].max) currentSplit[splitValue].max = curStr.trim().length;
			}
			currentSplit = currentSplit[splitValue].ary;
			isSplit      = true;
			curStr       = '';
			break;
		}
	}
}

function z(){ output += curStr.trim() + '\n'; }

doLoop(x);
doLoop(y, z);

stdout.Write(output.substr(0, output.length-1));